package org.cttic.visualization.visualization.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.cttic.visualization.visualization.common.IdentifyPointInfo;
import org.cttic.visualization.visualization.service.IdentifyPointService;
import org.cttic.visualization.visualization.util.ResponseObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class IdentifyPointServiceImpl implements IdentifyPointService {


    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public ResponseObject pointsInfo(Map<String, String> info) {
        ResponseObject response = ResponseObject.success();

        String provinceId = info.get("provinceId");
        if (StrUtil.isEmpty(provinceId)) {
            return response.error("省编码不能为空。");
        }
        String dataType = info.get("dataType");
        if ("1".equals(dataType)) {
            log.info("查询普通门架信息:{}", info.toString());
            response.setBizBody(gantryInfos(provinceId, 1));
        } else if ("2".equals(dataType)) {
            log.info("查询省界门架信息:{}", info.toString());
            response.setBizBody(gantryInfos(provinceId, 2));
        } else if ("3".equals(dataType)) {
            log.info("查询收费站信息:{}", info.toString());
            response.setBizBody(stationInfos(provinceId));
        } else {
            return response.error("未知的识别点类型。");
        }
        return response;
    }


    /**
     * 门架
     * @param provinceId
     * @param gantryType
     * @return
     */
    private JSONObject gantryInfos(String provinceId, int gantryType) {
        JSONObject jsonObject = new JSONObject();
        List<JSONObject> provinceList = new ArrayList<>();
        String[] split = provinceId.split("\\|");
        for (String s : split) {
            String sql = " SELECT gantryId tollGrantryId,gnclng lng,gnclat lat ,pileNumber stakeNum,gantryName,gantryType type, " +
                "roadSectionId sectionId,roadSectionName sectionName,roadId,roadName " +
                "FROM T_GANTRY_MOUNTING WHERE isAbnormal = 0  and provinceId = '" + s + "' and gantryType =" + gantryType;
            List<IdentifyPointInfo> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper(IdentifyPointInfo.class));

            JSONObject province = JSONUtil.createObj().put("provinceId", s)
                .put("dataList", query);
            provinceList.add(province);
        }


        jsonObject.put("provinceList", provinceList);
        return jsonObject;
    }

    /**
     * 收费站
     * @param provinceId
     * @return
     */
    private JSONObject stationInfos(String provinceId) {
        JSONObject jsonObject = new JSONObject();
        List<JSONObject> provinceList = new ArrayList<>();
        String[] split = provinceId.split("\\|");
        for (String s : split) {
            String sql = "SELECT stationId tollStationId,stationName `name`,gnclng lng,gnclat lat  " +
                "FROM `T_STATION` where isAbnormal = 0 and provinceId = " + s;
            List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
            JSONObject province = JSONUtil.createObj().put("provinceId", s)
                .put("dataList", maps);

            provinceList.add(province);
        }
        jsonObject.put("provinceList", provinceList);
        return jsonObject;
    }


}
